package com.droolsdemo.service;

import com.droolsdemo.dao.PersonDao;
import com.droolsdemo.pojo.Person;
import org.kie.api.KieBase;
import org.kie.api.runtime.KieSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PersonService {
    @Autowired
    private PersonDao personDao;

    @Autowired
    private DroolsService droolsService;

    public List<Person> listAll() {
        KieBase kieBase = droolsService.newKieBase();
        List<Person> people = personDao.listAll();
        for (Person person : people) {
            KieSession kieSession = kieBase.newKieSession();
            /**
             * 每一个person插入规则引擎后,触发规则后，被规则修改
             * 然后改变对应的属性
             */
            kieSession.insert(person);
            int i = kieSession.fireAllRules();
            kieSession.dispose();
        }
        return people;
    }
}
